package jgl.vajra.management.widget.mvvm.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import jgl.vajra.management.widget.base.BaseActivity;
import jgl.vajra.management.widget.base.BaseRefreshViewLayout;
import jgl.vajra.management.widget.mvvm.ActivityLiftCycle;
import jgl.vajra.management.widget.mvvm.factory.ViewModelFactory;
import jgl.vajra.management.widget.mvvm.factory.ViewModelFactoryImpl;
import jgl.vajra.management.widget.mvvm.proxy.BaseViewModelProxy;
import jgl.vajra.management.widget.mvvm.proxy.ViewModelProxyImpl;
import jgl.vajra.management.widget.mvvm.viewmodel.BaseViewModel;
import com.orhanobut.logger.Logger;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import butterknife.ButterKnife;

/**
 * @Author zhangshuqi
 * @CreateTime 2018/3/13
 * @Describe MVVM的基类, Proxy管理 View 与ViewModel的生命周期
 */

public abstract class BaseMVVMActivity<VM extends BaseViewModel, D extends ViewDataBinding> extends BaseActivity implements BaseViewModelProxy<VM>, ActivityLiftCycle, BaseMVVMView {
    private static final String VIEW_MODEL_SAVE_KEY = "view_model_save_key";
    /**
     * 创建被代理对象,传入默认Presenter的工厂
     */
    private ViewModelProxyImpl<VM> mProxy;
    protected D mBinding;
    protected VM mViewModel;

    @Override
    public void setViewModelFactory(ViewModelFactory factory) {
        mProxy.setViewModelFactory(factory);
    }


    @Override
    public ViewModelFactory getViewModelFactory() {
        return mProxy.getViewModelFactory();
    }

    @Override
    public VM getViewModel() {
        return mProxy.getViewModel();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getLayoutId() <= 0) {
            throw new NullPointerException("layout Id is null ");
        }
        mBinding = DataBindingUtil.setContentView(this, getLayoutId());
        if (isUseProxy()) {
            mProxy = new ViewModelProxyImpl<>(ViewModelFactoryImpl.<VM>createViewModelFactory(getClass()));
            mProxy.setViewBinding(mBinding);
            mViewModel = getViewModel();
            mProxy.onCreate(savedInstanceState);
            mProxy.setActivity(this);
            mProxy.onAttachViewModelView(this);

        }
        if (savedInstanceState != null)
            Logger.d("mProxy savedInstanceState" + mProxy == null);
        ButterKnife.bind(this);
        initView();
        initData();
        initEvent();
        registerEvent();
    }

    protected BaseRefreshViewLayout getRecyclerLayoutView() {
        return null;
    }


    public boolean isUseProxy() {
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isUseProxy())
            mProxy.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (isUseProxy())
            mProxy.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (isUseProxy())
            mProxy.onStop();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (isUseProxy())
            mProxy.onStart();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (isUseProxy())
            outState.putBundle(VIEW_MODEL_SAVE_KEY, mProxy.onSaveInstanceState());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isUseProxy())
            mProxy.onDestroy();
        if (getRecyclerLayoutView() != null)
            getRecyclerLayoutView().onDestroy();
        AppActivityManager.getAppActivityManager().finishActivity(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (isUseProxy())
            mProxy.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (isUseProxy()) {
            if (mProxy.onKeyDown(keyCode, event)) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        if (isUseProxy()) {
            if (!mProxy.onBackPressed()) {
                super.onBackPressed();
            }
        }
        super.onBackPressed();

    }


}
